package com.java1412.goods.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author java1412
 * @since 2025-05-15 11:18
 */
@RestController
@RequestMapping("api/v1/sentinel")
public class SentinelTestController {

    // volatile：修改的变量的值会立即同步到内存中
    private volatile int total = 0;

    @RequestMapping(value = "test-sentinel", produces = {"application/json;charset=utf-8"})
    public Object findByGoodsId() {
        return total++;
    }

    @GetMapping("test-sentinel-exception")
    public Object testSentinelException() {
        int i = (int) (Math.random() * 100);
        System.out.println(i);
        if (i > 30) {
            throw new NullPointerException("随机错误");
        }
        return "成功";
    }
}